//
//  RechargeListViewController.m
//  CBT
//
//  Created by Mr.Zhang on 2018/11/24.
//  Copyright © 2018 Mr.Zhang. All rights reserved.
//

#import "RechargeListViewController.h"

#import "RechargeListCell.h"
#import "WithdrawListCell.h"
#import "TransferCell.h"

#import "RechargeListViewModel.h"

@interface RechargeListViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NoDataView *noNetWorkView;
@property (nonatomic, strong) NoDataView *noDataView;
@property (nonatomic, strong) RechargeListViewModel *viewModel;

@property (nonatomic, copy  ) NSString *page;
@property (nonatomic, strong) NSMutableArray *dataArray;

@end

@implementation RechargeListViewController

#pragma mark - VC Life

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.page = @"1";
    
    self.navigationItem.title = kLocalizedString(self.showListType == 0 ? @"充值记录" : self.showListType == 1 ? @"挖矿记录" : self.showListType == 2 ? @"划转记录" : @"转账记录", nil);
    
    [self initTableViewInfo];
    [self outPutAction];
}

- (void)initTableViewInfo
{
    self.tableView.backgroundColor = UIColorFromRGB(0xf1f1f1);
    
    if (self.showListType == 2 || self.showListType == 3) {
        self.tableView.separatorStyle = UITableViewCellEditingStyleNone;
        self.tableView.contentInset = UIEdgeInsetsMake(4, 0, 0, 0);
    }
    
    self.tableView.tableFooterView = [UIView new];
    
    if (@available(iOS 11.0, *)){
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }else{
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    self.tableView.estimatedRowHeight           = 60;
    self.tableView.estimatedSectionHeaderHeight = 0;
    self.tableView.estimatedSectionFooterHeight = 0;
    
    [self.tableView registerNib:[UINib nibWithNibName:@"WithdrawListCell" bundle:nil] forCellReuseIdentifier:@"listCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"TransferCell" bundle:nil] forCellReuseIdentifier:@"TransferID"];
    
    __weak RechargeListViewController *weakSelf = self;
    _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        weakSelf.page = @"1";
        [weakSelf.viewModel exchangeDataWithPage:weakSelf.page];
    }];
    
    _tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        [weakSelf.viewModel exchangeDataWithPage:weakSelf.page];
    }];
    
    _tableView.mj_footer.hidden = YES;
    
    [_tableView.mj_header beginRefreshing];
}

#pragma mark - OutPut
- (void)outPutAction {
    @weakify(self);
    //列表请求成功
    [self.viewModel.listSuccessObject subscribeNext:^(id data) {
        if (self_weak_.showListType == 0) {
            RechargeRecordModel *model = data;
            
            if ([self_weak_.page intValue] == 1) {
                self_weak_.dataArray = [NSMutableArray array];
            }
            
            [self_weak_.noNetWorkView removeFromSuperview];
            if (model.rechargeRecord.count == 0 && [NSArray isArrayEmpty:self_weak_.dataArray]) {
                [self_weak_.tableView addSubview:self_weak_.noDataView];
            }
            else {
                [self_weak_.dataArray addObjectsFromArray:model.rechargeRecord];
                self_weak_.page = [NSString stringWithFormat:@"%d",[self_weak_.page intValue] + 1];
                self_weak_.tableView.mj_footer.hidden = NO;
            }
            if ([self_weak_.page intValue] > [model.totalPage intValue]) {
                [self_weak_.tableView.mj_header endRefreshing];
                [self_weak_.tableView.mj_footer endRefreshingWithNoMoreData];
            }
            else {
                [self_weak_.tableView.mj_header endRefreshing];
                [self_weak_.tableView.mj_footer endRefreshing];
            }
        }
        else if(self_weak_.showListType == 1) {
            RechargeListModel *model = data;
            
            if ([self_weak_.page intValue] == 1) {
                self_weak_.dataArray = [NSMutableArray array];
            }
            
            [self_weak_.noNetWorkView removeFromSuperview];
            if (model.investRecord.count == 0 && [NSArray isArrayEmpty:self_weak_.dataArray]) {
                [self_weak_.tableView addSubview:self_weak_.noDataView];
            }
            else {
                self_weak_.tableView.mj_footer.hidden = NO;
                [self_weak_.dataArray addObjectsFromArray:model.investRecord];
                self_weak_.page = [NSString stringWithFormat:@"%d",[self_weak_.page intValue] + 1];
            }
            if ([self_weak_.page intValue] > [model.totalPage intValue]) {
                [self_weak_.tableView.mj_header endRefreshing];
                [self_weak_.tableView.mj_footer endRefreshingWithNoMoreData];
            }
            else {
                [self_weak_.tableView.mj_header endRefreshing];
                [self_weak_.tableView.mj_footer endRefreshing];
            }
        }
        else if(self_weak_.showListType == 2) {
            WithdrawListModel *model = data;
            
            if ([self_weak_.page intValue] == 1) {
                self_weak_.dataArray = [NSMutableArray array];
            }
            
            [self_weak_.noNetWorkView removeFromSuperview];
            if (model.drawRecord.count == 0 && [NSArray isArrayEmpty:self_weak_.dataArray]) {
                [self_weak_.tableView addSubview:self_weak_.noDataView];
            }
            else {
                self_weak_.tableView.mj_footer.hidden = NO;
                [self_weak_.dataArray addObjectsFromArray:model.drawRecord];
                self_weak_.page = [NSString stringWithFormat:@"%d",[self_weak_.page intValue] + 1];
            }
            if ([self_weak_.page intValue] > [model.totalPage intValue]) {
                [self_weak_.tableView.mj_header endRefreshing];
                [self_weak_.tableView.mj_footer endRefreshingWithNoMoreData];
            }
            else {
                [self_weak_.tableView.mj_header endRefreshing];
                [self_weak_.tableView.mj_footer endRefreshing];
            }
        }
        else {
            TransferModel *model = data;
            
            if ([self_weak_.page intValue] == 1) {
                self_weak_.dataArray = [NSMutableArray array];
            }
            
            [self_weak_.noNetWorkView removeFromSuperview];
            if (model.changeRecord.count == 0 && [NSArray isArrayEmpty:self_weak_.dataArray]) {
                [self_weak_.tableView addSubview:self_weak_.noDataView];
            }
            else {
                self_weak_.tableView.mj_footer.hidden = NO;
                [self_weak_.dataArray addObjectsFromArray:model.changeRecord];
                self_weak_.page = [NSString stringWithFormat:@"%d",[self_weak_.page intValue] + 1];
            }
            if ([self_weak_.page intValue] > [model.totalPage intValue]) {
                [self_weak_.tableView.mj_header endRefreshing];
                [self_weak_.tableView.mj_footer endRefreshingWithNoMoreData];
            }
            else {
                [self_weak_.tableView.mj_header endRefreshing];
                [self_weak_.tableView.mj_footer endRefreshing];
            }
        }
        
        [self_weak_.tableView reloadData];
    }];
    
    //列表请求失败
    [self.viewModel.listFailureObject subscribeNext:^(id x) {
        [HFStatusView showFailureMessage:kLocalizedString(@"网络异常，请稍后重试", nil)];
        [self_weak_ stopLoading];
        [self_weak_.tableView.mj_header endRefreshing];
        [self_weak_.tableView.mj_footer endRefreshing];
        if ([self_weak_.page intValue] == 1 && [NSArray isArrayEmpty:self_weak_.dataArray]) {
            self_weak_.tableView.mj_footer.hidden = YES;
            [self_weak_.noDataView removeFromSuperview];
            [self_weak_.tableView addSubview:self_weak_.noNetWorkView];
        }
        else {
            self_weak_.tableView.mj_footer.hidden = NO;
            [self_weak_.noNetWorkView removeFromSuperview];
        }
        [self_weak_.tableView reloadData];
    }];
}


#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.showListType == 2 || self.showListType == 3) {
        TransferCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TransferID"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.showListType = self.showListType;
        cell.symbol = self.coinName;
        if (self.showListType == 2) {
            cell.withdrawModel = self.dataArray[indexPath.row];
        }
        else {
            ((TransferItemModel *)self.dataArray[indexPath.row]).coinName = self.coinName;
            cell.model = self.dataArray[indexPath.row];
        }
        return cell;
    }
    else {
        RechargeListCell *listCell = [tableView dequeueReusableCellWithIdentifier:@"ZJCell"];
        listCell.selectionStyle = UITableViewCellSelectionStyleNone;
        listCell.symbol = self.coinName;

        if (self.showListType == 1) {
            listCell.model = self.dataArray[indexPath.row];
        }
        else {
            listCell.rechargeModel = self.dataArray[indexPath.row];
        }
        return listCell;
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return self.showListType == 2 || self.showListType == 3 ? 83 : 60;
}

#pragma mark - Lazy

- (NoDataView *)noDataView
{
    if (!_noDataView) {
        _noDataView = [[NoDataView alloc]initWithFrame:self.view.bounds type:(ShowType_NoData) clickBlock:^{}];
    }
    return _noDataView;
}

- (NoDataView *)noNetWorkView
{
    if (!_noNetWorkView) {
        @weakify(self);
        _noNetWorkView = [[NoDataView alloc]initWithFrame:self.view.bounds type:(ShowType_NoNetWork) clickBlock:^{
            [self_weak_ startClearBgColorLoading];
            [self_weak_.noDataView removeFromSuperview];
            [self_weak_.noNetWorkView removeFromSuperview];
            self_weak_.page = @"1";
            [self_weak_.viewModel exchangeDataWithPage:self_weak_.page];
        }];
    }
    return _noNetWorkView;
}

- (NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

- (RechargeListViewModel *)viewModel
{
    if (!_viewModel) {
        _viewModel = [[RechargeListViewModel alloc] init];
        _viewModel.showListType = self.showListType;
        _viewModel.coinId = self.coinId;
    }
    return _viewModel;
}


@end
